import { showModal, showToast } from "@/utils/uni-api.ts";

let BASE_URL = "https://tea.qingnian8.com/api/bizhi";

//网络请求
export function request(obj : any = {}) {
    let { url, method = "GET", header = {}, data = {} } = obj;
    url = BASE_URL + url;
    header["access-key"] = "";

    return new Promise((reslove, reject) => {
        uni.request({
            url: url,
            data: data,
            method: method,
            header: header,
            //调用成功回调
            success: (res : any) => {
                if (res.data.errCode === 0) {
                    reslove(res.data);
                } else if (res.data.errCode === 400) {
                    showModal({ title: "异常信息", content: res.data.errMsg, showCancel: false });
                    reject(res.data);
                } else {
                    showToast("请求异常");
                    reject(res.data);
                };
            },
            //调用失败回调
            fail: err => {
                showToast("请求异常");
                reject(err);
            }
        });
    });
};